public without sharing class BloodSugarOrderHander {
    
    //报价审批通过以后，把其它同类型的报价变为无效
    public static void disableOtherBlood(List<BloodSugarOrder__c> newList, Map<Id, BloodSugarOrder__c> oldMap) {
        for (BloodSugarOrder__c bso  : newList) {
            BloodSugarOrder__c old = oldMap.get(bso.Id);
            if (old.Status__c!= bso.Status__c && bso.Status__c == '审批成功' ) {
                
               // if(bso.Contract__c != null && bso.FromOpp__c != true) contractBack(bso);
                contractBack(bso );

                // if(bso.FromOpp__c != true  ) continue;
                // bso.Effect__c = true;
                // // FIXME 审批只会一条一条来，所以允许在for循环里面写soql
                // Opportunity opp = [select Id, RecordType.Name, ExecuteCompany__r.RecordType.DeveloperName from Opportunity where Id = :bso.Opportunities__c];
                // List<BloodSugarOrder__c> otherQList = [select Id,Effect__c from BloodSugarOrder__c where Opportunities__c = :bso.Opportunities__c  and Id != :bso.Id  and Effect__c = true];
                // if (otherQList.size() > 0) {
                //     for (BloodSugarOrder__c oq : otherQList) {
                //         oq.Effect__c = false;
                //     }
                //     update otherQList;
                // }


            }
        }
    }

    public static void contractBack(BloodSugarOrder__c bos ){


        Map<Id,ContractProduct__c> cpMap =new Map<Id,ContractProduct__c>();

        //Set<Id> exsets = new Set<Id>();
        for(BloodSugarOrderDetail__c bd: [select Id,DistributionProduct__c,RelevantContractDetails__c ,BloodSugarOrder__r.Contract__c ,BloodSugarOrder__r.Opportunities__c  ,DistributionProduct__r.Name ,DistributionProductNumber__c  from BloodSugarOrderDetail__c where BloodSugarOrder__c =:bos.Id  and BloodSugarOrder__r.RecordType.DeveloperName in ('PolicyWithoutContract','ContinueOrdering')  ]){
          
          
           ContractProduct__c cp =  new ContractProduct__c();
           cp.OriginNumber__c = bd.DistributionProductNumber__c;
           cp.Product__c = bd.DistributionProduct__c;
           cp.Quantity__c = bd.DistributionProductNumber__c;

          
           cp.Name = bd.DistributionProduct__r.Name ;
           cp.ApplicationQuantity__c = bd.DistributionProductNumber__c;

           if(bos.Opportunities__c != null )cp.Opportunity__c = bos.Opportunities__c;
           // cp.Contract__c = bd.BloodSugarOrder__r.Contract__c;
           if(bos.Contract__c != null )cp.Contract__c =  bos.Contract__c;

           cp.BloodSugarOrderDetail1__c = bd.Id ;
           cp.Id = bd.RelevantContractDetails__c ; 
           cp.CoopType__c = '正常销售';
           cp.ApplyQuantity__c = bd.DistributionProductNumber__c;
           cp.Id = bd.RelevantContractDetails__c ;
           cp.CoCustomer__c = bos.Account__c;

           cpMap.put(bd.Id , cp);
           //if(bd.RelevantContractDetails__c != null)exsets.add(bd.RelevantContractDetails__c);
        }
        if(!cpMap.isEmpty())upsert  cpMap.values();
        System.debug( cpMap);

        List<BloodSugarOrderDetail__c> bdlist =new List<BloodSugarOrderDetail__c>();

        for (Id bdId  : cpMap.keySet()) {

            bdlist.add(new BloodSugarOrderDetail__c(Id=bdId ,RelevantContractDetails__c=cpMap.get(bdId).Id ));
        }

        System.debug( bdlist);

        if(!bdlist.isEmpty()) update bdlist;
    }
}